<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 1. new XMLHttpRequest
        let xhr = new XMLHttpRequest()
        // 2. 设置请求方式和url
        xhr.open('post', 'http://ajax-api.itheima.net/api/books')

        // 根据接口文档的要求，设置请求头content-type
        xhr.setRequestHeader('content-type', 'application/json')

        // 3. 发送
        // send(请求体的内容)
        const d = {bookname: '书名', author: '作者', publisher: '出版社'}
        xhr.send(JSON.stringify(d))
        // 4. 监听load事件，取到服务器回来的数据
        xhr.addEventListener('load', () => {
            console.log(xhr.response)
        })
    </script>
</body>
</html>